package com.campus.help.exception;

import org.springframework.boot.web.servlet.error.ErrorController;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.RequestDispatcher;
import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;

/**
 * 自定义错误控制器
 * 替代默认的Whitelabel错误页面
 */
@Controller
public class CustomErrorController implements ErrorController {

    /**
     * 处理所有错误请求
     * @param request HTTP请求
     * @return 自定义错误页面
     */
    @RequestMapping("/error")
    public ResponseEntity<String> handleError(HttpServletRequest request) {
        Object status = request.getAttribute(RequestDispatcher.ERROR_STATUS_CODE);
        Object message = request.getAttribute(RequestDispatcher.ERROR_MESSAGE);
        
        int statusCode = 500;
        if (status != null) {
            statusCode = Integer.parseInt(status.toString());
        }
        
        String errorTitle = "服务器错误";
        String errorDescription = "处理您的请求时发生了错误";
        String errorIcon = "⚠️";
        String errorColor = "#ea4335"; // 默认红色
        
        if (statusCode == 404) {
            errorTitle = "页面未找到";
            errorDescription = "您请求的页面不存在";
            errorIcon = "🔍";
            errorColor = "#4285f4"; // 蓝色
        } else if (statusCode == 403) {
            errorTitle = "禁止访问";
            errorDescription = "您没有权限访问此资源";
            errorIcon = "🔒";
            errorColor = "#fbbc05"; // 黄色
        } else if (statusCode == 500) {
            errorTitle = "服务器错误";
            errorDescription = "服务器内部错误";
            errorIcon = "⚠️";
            errorColor = "#ea4335"; // 红色
        }
        
        String errorMessage = message != null ? message.toString() : "未知错误";
        
        String htmlResponse = "<!DOCTYPE html>" +
                "<html lang='zh-CN'>" +
                "<head>" +
                "    <meta charset='UTF-8'>" +
                "    <meta name='viewport' content='width=device-width, initial-scale=1.0'>" +
                "    <title>错误 " + statusCode + " - 校园互助系统</title>" +
                "    <style>" +
                "        :root {" +
                "            --primary-color: #4285f4;" +
                "            --secondary-color: #34a853;" +
                "            --accent-color: #fbbc05;" +
                "            --danger-color: " + errorColor + ";" +
                "            --light-color: #f8f9fa;" +
                "            --dark-color: #202124;" +
                "        }" +
                "        * { margin: 0; padding: 0; box-sizing: border-box; }" +
                "        body {" +
                "            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;" +
                "            line-height: 1.6;" +
                "            color: var(--dark-color);" +
                "            background-color: #f5f5f5;" +
                "            padding: 0;" +
                "            margin: 0;" +
                "            min-height: 100vh;" +
                "            display: flex;" +
                "            flex-direction: column;" +
                "            justify-content: center;" +
                "        }" +
                "        .error-container {" +
                "            max-width: 800px;" +
                "            margin: 40px auto;" +
                "            padding: 40px;" +
                "            background-color: white;" +
                "            border-radius: 8px;" +
                "            box-shadow: 0 4px 15px rgba(0,0,0,0.1);" +
                "        }" +
                "        .error-header {" +
                "            display: flex;" +
                "            align-items: center;" +
                "            margin-bottom: 30px;" +
                "            padding-bottom: 20px;" +
                "            border-bottom: 1px solid var(--light-color);" +
                "        }" +
                "        .error-icon {" +
                "            font-size: 3.5rem;" +
                "            margin-right: 20px;" +
                "            color: var(--danger-color);" +
                "        }" +
                "        .error-title {" +
                "            flex: 1;" +
                "        }" +
                "        .error-title h1 {" +
                "            color: var(--danger-color);" +
                "            font-size: 2.2rem;" +
                "            margin-bottom: 5px;" +
                "        }" +
                "        .error-title p {" +
                "            font-size: 1.1rem;" +
                "            color: #666;" +
                "        }" +
                "        .error-details {" +
                "            background-color: var(--light-color);" +
                "            padding: 20px;" +
                "            border-radius: 6px;" +
                "            margin-bottom: 30px;" +
                "        }" +
                "        .error-details p {" +
                "            margin-bottom: 10px;" +
                "        }" +
                "        .error-details .timestamp {" +
                "            font-size: 0.9rem;" +
                "            color: #666;" +
                "            text-align: right;" +
                "            margin-top: 15px;" +
                "        }" +
                "        .solutions {" +
                "            margin-top: 30px;" +
                "        }" +
                "        .solutions h3 {" +
                "            color: var(--primary-color);" +
                "            margin-bottom: 15px;" +
                "            font-size: 1.3rem;" +
                "        }" +
                "        .solutions ul {" +
                "            list-style-type: none;" +
                "        }" +
                "        .solutions li {" +
                "            margin-bottom: 12px;" +
                "            padding: 12px;" +
                "            background-color: var(--light-color);" +
                "            border-radius: 6px;" +
                "            position: relative;" +
                "            padding-left: 30px;" +
                "        }" +
                "        .solutions li:before {" +
                "            content: '✓';" +
                "            position: absolute;" +
                "            left: 10px;" +
                "            color: var(--secondary-color);" +
                "            font-weight: bold;" +
                "        }" +
                "        .solutions a {" +
                "            color: var(--primary-color);" +
                "            text-decoration: none;" +
                "        }" +
                "        .solutions a:hover {" +
                "            text-decoration: underline;" +
                "        }" +
                "        .btn {" +
                "            display: inline-block;" +
                "            background-color: var(--primary-color);" +
                "            color: white;" +
                "            padding: 12px 24px;" +
                "            border-radius: 4px;" +
                "            text-decoration: none;" +
                "            transition: background-color 0.3s ease;" +
                "            font-weight: bold;" +
                "            margin-top: 20px;" +
                "        }" +
                "        .btn:hover {" +
                "            background-color: #3367d6;" +
                "        }" +
                "        .actions {" +
                "            margin-top: 30px;" +
                "            text-align: center;" +
                "        }" +
                "    </style>" +
                "</head>" +
                "<body>" +
                "    <div class='error-container'>" +
                "        <div class='error-header'>" +
                "            <div class='error-icon'>" + errorIcon + "</div>" +
                "            <div class='error-title'>" +
                "                <h1>" + errorTitle + " - " + statusCode + "</h1>" +
                "                <p>" + errorDescription + "</p>" +
                "            </div>" +
                "        </div>" +
                "        <div class='error-details'>" +
                "            <p><strong>错误信息:</strong> " + errorMessage + "</p>" +
                "            <p class='timestamp'>时间: " + new Date() + "</p>" +
                "        </div>" +
                "        <div class='solutions'>" +
                "            <h3>可能的解决方案:</h3>" +
                "            <ul>" +
                "                <li>检查URL是否正确，确保包含上下文路径 <code>/campus-help</code></li>" +
                "                <li>确认您使用了正确的HTTP方法 (GET, POST, PUT, DELETE)</li>" +
                "                <li>查看API文档了解正确的请求格式和参数</li>" +
                "                <li>确保请求头中包含了必要的认证信息</li>" +
                "            </ul>" +
                "        </div>" +
                "        <div class='actions'>" +
                "            <a href='/campus-help/' class='btn'>返回首页</a>" +
                "        </div>" +
                "    </div>" +
                "</body>" +
                "</html>";
        
        return ResponseEntity
                .status(statusCode)
                .contentType(MediaType.TEXT_HTML)
                .body(htmlResponse);
    }
}